﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.order
{
    public partial class orderProcess : BaseCrm 
    {
        readonly t_DALOrderProcess theOrderProcess = new t_DALOrderProcess();
        readonly t_DALOrder theOrder = new t_DALOrder();
        t_Order oOrder;
        t_OrderProcess oOrderProcess;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(eRule.OrderManager);
                this.BindOrderStates();
            }
        }

        void BindOrderStates()
        {
           oOrder = theOrder.GetModel(int.Parse(Request.QueryString["id"]));
            if(oOrder != null)
            {
                if (oOrder.OrderStates == 0)
                {
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("新订单", "0"));
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("备货中", "1"));
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("已完成", "2"));
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("已取消", "3"));
                }
                else if (oOrder.OrderStates == 1)
                {
               
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("备货中", "1"));
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("已完成", "2"));
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("已取消", "3"));
                }
                else if (oOrder.OrderStates == 2)
                {
                    this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("已完成", "2"));

                }
                this.ddlOrderStates.Items.Insert(0, new ExtAspNet.ListItem("选择订单状态", "-1"));
                this.ddlOrderStates.SelectedValue = "-1";
            }
        }

    
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
             oOrder = theOrder.GetModel(int.Parse(Request.QueryString["id"]));
             if (oOrder != null)
             {
                 string sOldStates = "";
                 string sNewStates = "";
                 if(oOrder.OrderStates == 0)
                 {
                     sOldStates = "新订单";
                 }else if(oOrder.OrderStates == 1)
                 {
                     sOldStates = "备货中";
                 }else if(oOrder.OrderStates == 2)
                 {
                     sOldStates = "已完成";
                 }
                 else if (oOrder.OrderStates == 3)
                 {
                     sOldStates = "已取消";
                 }

                 oOrder.OrderStates = int.Parse(this.ddlOrderStates.SelectedItem.Value);
                 
                  if(oOrder.OrderStates == 0)
                 {
                     sNewStates = "新订单";
                 }else if(oOrder.OrderStates == 1)
                 {
                     sNewStates = "备货中";
                 }else if(oOrder.OrderStates == 2)
                 {
                     sNewStates = "已完成";
                 }
                  else if (oOrder.OrderStates == 3)
                  {
                      sNewStates = "已取消";
                  }

                 oOrderProcess = new t_OrderProcess();
                 oOrderProcess.orderId = oOrder.id;
                 oOrderProcess.creater = int.Parse(base.LoginAdminId);
                 oOrderProcess.createTime = DateTime.Now;
                 if (this.txtRemark.Text.Trim() == "")
                 {
                     oOrderProcess.processRemark = string.Format("订单状态由({0})更新为({1})"
                                                                   , sOldStates
                                                                   , sNewStates);

                 }
                 else
                 {
                     oOrderProcess.processRemark = this.txtRemark.Text.Trim() + ";<br/>"
                                                + string.Format("{2}更新订单状态由({0})到({1})"
                                                                  , sOldStates
                                                                  , sNewStates
                                                                  , base.AdminUser);
                 }
                 theOrder.Update(oOrder);
                 theOrderProcess.Add(oOrderProcess);
                 ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
             }
        }
    }
}